/* 
    前端后端不分离--服务端（后端）渲染 -页面

    后端生成页面
*/

let http = require("http");
let server = http.createServer((req, res) => {
  if (req.url == "/") {
    //返回首页 html页面
    res.writeHead(200, { "content-type": "text/html;charset=utf-8;" });

    res.write(`<h2>首页</h2>`);
    res.end();
  } else if (req.url == "/news") {
    //返回新闻 html页面
    let list = [{ title: "a" }, { title: "b" }];
    let liStr = list.map((v) => `<li>${v.title}</li>`);
    res.writeHead(200, { "content-type": "text/html;charset=utf-8;" });
    res.write(`<h2>新闻页面</h2> <ul>${liStr}</ul>`);
    res.end();
  }
});
server.listen(3000, function () {
  console.log("http://127.0.0.1:3000");
});
